home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / test / bsearch.c < prev    next >
C/C++ Source or Header  |  1992-11-23  |  957b  |  65 lines

  1.  
  2. /*
  3.  *  TEST BSEARCH
  4.  */
  5.  
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. #define ELEMENTS    100
  10.  
  11. #if TEST1
  12. typedef struct Fubar {
  13.     short   fu_Key;
  14.     short   fu_Magic;
  15. } Fubar;
  16. #elif TEST2
  17. typedef struct Fubar {        /*    not a power of 2    */
  18.     short   fu_Key;
  19.     short   fu_Magic;
  20.     char    fu[36];
  21. } Fubar;
  22. #endif
  23.  
  24.  
  25. Fubar    Ary[ELEMENTS];
  26.  
  27. int
  28. mycmp(key, elm)
  29. Fubar *key;
  30. Fubar *elm;
  31. {
  32.     if (key->fu_Key < elm->fu_Key)
  33.     return(-1);
  34.     if (key->fu_Key == elm->fu_Key)
  35.     return(0);
  36.     return(1);
  37. }
  38.  
  39. main(ac, av)
  40. char *av[];
  41. {
  42.     short i;
  43.     Fubar key;
  44.     Fubar *res;
  45.  
  46.     /*
  47.      *    fill array
  48.      */
  49.  
  50.     for (i = 0; i < ELEMENTS; ++i) {
  51.     Ary[i].fu_Key = i;
  52.     Ary[i].fu_Magic = 0x5555;
  53.     }
  54.     for (i = -10; i < ELEMENTS + 10; ++i) {
  55.     key.fu_Key = i;
  56.  
  57.     res = bsearch(&key, Ary, ELEMENTS, sizeof(Fubar), mycmp);
  58.     if (res)
  59.         printf("%d : %d %04x\n", i, res->fu_Key, res->fu_Magic);
  60.     else
  61.         printf("%d : not found\n", i);
  62.     }
  63.     return(0);
  64. }
  65.